package pl.edu.agh.jemo.evolution.objfunc.impl.clustering;

import java.util.Map;

import javax.swing.JPanel;

import pl.edu.agh.jemo.evolution.objfunc.CrowdingObjectiveFunction;
import pl.edu.agh.jemo.evolution.specimen.Specimen;

public class ClusteringObjectiveFunction extends CrowdingObjectiveFunction {

	private Map<Specimen, Double> clusterSpecimenMap;

	@Override
	protected void populationChanged() {
		Clusterizer clusterizer = new Clusterizer();
		clusterizer.setPopulationReference(population);
		clusterSpecimenMap = clusterizer.clusterize();

/*		centroidValues = new HashMap<Specimen, Double>();
		for (Window w : distanceMap.values()) {
			if (w == null)
				continue;
			Population pop = new Population();
			pop.addAll(w.getSpecimensInside());
			
			pop.setObjectiveFunctionSet(population
					.getObjectiveFunctionSet());
			PopulationCentroidObjectiveFunction fun = new PopulationCentroidObjectiveFunction();
			Collections.sort(pop, Population.getSpecimenPhenotype0Comparator());
			fun.setPopulationReference(pop);
			Double[] newCenter = new Double[pop.get(0).getGenotype().length];
			for (int i = 0 ; i < newCenter.length; i++) {
				newCenter[i] = pop.get(0).getGenotype()[i].asDouble();
			}
			fun.setCenter(newCenter);

			for (Specimen s : pop) {
				centroidValues.put(s, fun.calculateValue(s));
			}
			Double max = 0.;
			
			for (Double val : centroidValues.values()) {
				if (max < val) {
					max = val;
				}
			}
			
			for (Specimen s : pop) {
				centroidValues.put(s, 1000 * centroidValues.get(s) / max);
			}
			
		}*/
	}

	@Override
	public Double calculateValue(Specimen specimen) {

/*		if (clusterSpecimenMap.get(specimen) == null)
			// pytanie: co zrobic dla osobnikow bez klastra ?
			return 1000.;
		return centroidValues.get(specimen);*/
		if (!clusterSpecimenMap.containsKey(specimen)) {
			System.err.println("nie przypisalo value dla osobnikow spoza klastra.");
		}
		return clusterSpecimenMap.get(specimen);
	}

	@Override
	public JPanel getConfigPanel() {
		
		return null;
	}

	@Override
	public Double getMaxValue() {
		
		return null;
	}

	@Override
	public Double getMinValue() {
		
		return null;
	}
}
